  /**************************************************************************/
  /*                                                                        */
  /*  System  Name :  ICIM                                                  */
  /*                                                                        */
  /*  Description  :  Material Product Management                           */
  /*                                                                        */
  /*  MODIFICATION HISTORY                                                  */
  /*    Date     Ver     Name          Description                          */
  /* ---------- ----- ----------- ----------------------------------------- */
  /* 2017/09/12 N0.00                     Initial release                   */
  /*                                                                        */
  /**************************************************************************/

$(document).ready(function() {
	$("form").submit(function() {
		return false;
	});
	
    /**
     * All controls's jquery object/text
     * @type {Object}
     */
    var domObjs = {
        W                   : $(window)       ,
        grids : {
            $leftDiv         : $(".leftDiv"),
            $rightDiv		: $(".rightDiv"),
            $mtrlListDiv : $("#mtrlListDiv"),
            $mtrlListGrd : $("#mtrlListGrd")
        }
    };
    
	var inObj_Query;
	var domObj = {
		$mtrlProdIDTxt : $("#mtrlProdIDTxt"),
		$mtrlProdDscTxt : $("#mtrlProdDscTxt"),
		$mtrlCateSel : $("#mtrlCateSel"),
		$mtrlMkrSel : $("#mtrlMkrSel"),
		$mtrlChgCateSel : $("#mtrlChgCateSel"),
		$mtrlUseModeSel : $("#mtrlUseModeSel"),
		$mtrlUnitSel : $("#mtrlUnitSel"),
		$mtrlRunModeSel : $("#mtrlRunModeSel"),
		$mtrlQRSCateSel : $("#mtrlQRSCateSel"),
		$mtrlStdWgtTxt : $("#mtrlStdWgtTxt"),
		$mtrlEmpyWgtTxt : $("#mtrlEmpyWgtTxt"),
		$mtrlSafeWgtTxt : $("#mtrlSafeQtyTxt"),
		grids : {
			$mtrlListGrd : $("#mtrlListGrd"),
			mtrlListGrdSelector : "#$mtrlListGrd",
			$mtrlListDiv : $("#mtrlListDiv"),
			mtrlListPg : "#mtrlListPg"
		},
		buttons : {
			$query_btn : $("#query_btn"),
			$delete_btn : $("#delete_btn"),
			$update_btn : $("#update_btn"),
			$add_btn : $("#add_btn"),
			$save_btn : $("#save_btn"),
			$clear_btn : $("#clear_btn"),
			$copy_btn : $("#copy_btn"),
			$rollback_btn : $("#rollback_btn"),
			$addMtrlMkrBtn : $("#addMtrlMkrBtn"),
			$deleteMtrlMkrBtn : $("#deleteMtrlMkrBtn")
		},
		dialogs : {
			$queryMtrlDialog : $("#queryMtrlDialog"),
			$queryMtrlDialog_queryBtn : $("#queryMtrlDialog_queryBtn"),
			$queryMtrlDialog_mtrlProdIDTxt : $("#queryMtrlDialog_mtrlProdIDTxt")
		}

	};
	var VAL = {
		NORMAL : "0000000",
		EVT_USR : $("#userId").text(),
		DISABLED_ATTR : {
			'disabled' : true
		},
		ENABLED_ATTR : {
			'disabled' : false
		},
		T_FBPBISMTR : "FBPBISMTR",
		T_FBPBISCUS : "FBPBISCUS",
		ACTION_ADD : "A",
		ACTION_UPDATE : "U"
	};
	var globalObj = {
		cusdOaryAry : {}
	};

	var CheckBoxDom = {
		setCheckBox : function($domObj, checkFlg) {
			if (checkFlg) {
				$domObj.attr("checked", "true");
			} else {
				$domObj.removeAttr("checked");
			}
		},
		isChecked : function($domObj) {
			return ($domObj.attr("checked") === "checked" ? true : false);
		}
	};

	domObj.grids.$mtrlListGrd.jqGrid({
		url : "",
		datatype : "local",
		mtype : "POST",
        autowidth : true,//宽度根据父元素自适应
        shrinkToFit:false,
        scroll: true,
		resizable : true,
		loadonce : true,
		fixed : true,
		viewrecords : true, // 显示总记录数
		pager : domObj.grids.mtrlListPg,
		rownumbers : true,// 显示行号
		colModel : [{
					name : 'mtrl_prod_id',
					index : 'mtrl_prod_id',
					label : MTRL_PRODUCT_ID_TAG,
					width : 230
				}, {
					name : 'mtrl_prod_dsc',
					index : 'mtrl_prod_dsc',
					label : "原料描述",
					width : 270
				}, {
					name : 'data_seq_id',
					index : 'data_seq_id',
					label : "主键",
					width : 300,
					hidden : true
				}],
		onSelectRow : function(id) {
			if(domObj.$mtrlProdDscTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "目前处于编辑中，请勿进行其它操作");
				return false;
			}
			mtrlGridSelRowFnc(id);
		}
	});
	
    //jqGrid表格自适应
    function resizeFnc(){
        var grdDivHeight,grdDivWidth;
        grdDivWidth = domObjs.W.width() - 120 - domObjs.grids.$rightDiv.width();
        grdDivHeight = domObjs.W.height() - domObjs.grids.$leftDiv.offset().top-51;

        domObjs.grids.$leftDiv.height(grdDivHeight);
        domObjs.grids.$mtrlListDiv.width(grdDivWidth);
        domObjs.grids.$mtrlListDiv.height(grdDivHeight*0.99 - 51);
        domObjs.grids.$mtrlListGrd.setGridWidth(grdDivWidth*0.99);
        domObjs.grids.$mtrlListGrd.setGridHeight(grdDivHeight*0.99 - 101);
    };

    resizeFnc();
    
	function mtrlGridSelRowFnc(id){
		var rowData, mtrl_product_id, iary, inObj, outObj, oary, tblCnt, i, j, cusdOary, cusdCnt;

		$("input").attr(VAL.DISABLED_ATTR);
		$("select").attr(VAL.DISABLED_ATTR);

		rowData = domObj.grids.$mtrlListGrd.jqGrid("getRowData", id);
		var inObj = {
			trx_id : VAL.T_FBPBISMTR,
			action_flg : "Q",
			iary : [{
				mtrl_prod_id : rowData.mtrl_prod_id
			}],
			tbl_cnt : 1
		};
		outObj = comTrxSubSendPostJson(inObj);
		if (outObj.rtn_code !== VAL.NORMAL) {
			return false;
		}
		tblCnt = outObj.tbl_cnt;
		oary = outObj.oary[0];
		if(!oary){
			showErrorDialog("", "该物料不存在，请确认！");
			return false;
		}
		
		$("input").val("");
		domObj.$mtrlProdIDTxt.val(oary.mtrl_prod_id);
		domObj.$mtrlProdDscTxt.val(oary.mtrl_prod_dsc);
	    SelectDom.initWithSpace(domObj.$mtrlCateSel);
		SelectDom.setSelect(domObj.$mtrlCateSel, oary.mtrl_cate);
		SelectDom.initWithSpace(domObj.$mtrlMkrSel);
		SelectDom.setSelect(domObj.$mtrlMkrSel, oary.mtrl_mkr);
		SelectDom.initWithSpace(domObj.$mtrlChgCateSel);
		SelectDom.setSelect(domObj.$mtrlChgCateSel, oary.mtrl_chg_cate);
		SelectDom.initWithSpace(domObj.$mtrlUseModeSel);
		SelectDom.setSelect(domObj.$mtrlUseModeSel, oary.mtrl_use_mode);
		SelectDom.initWithSpace(domObj.$mtrlQRSCateSel);
		SelectDom.setSelect(domObj.$mtrlQRSCateSel, oary.mtrl_qrs_cate);
		SelectDom.initWithSpace(domObj.$mtrlRunModeSel);
		SelectDom.setSelect(domObj.$mtrlRunModeSel, oary.mtrl_use_tool_run_mode);
		SelectDom.initWithSpace(domObj.$mtrlUnitSel);
		SelectDom.setSelect(domObj.$mtrlUnitSel, oary.mtrl_unit);
		domObj.$mtrlSafeWgtTxt.val(oary.mtrl_safe_stk_qty);
		domObj.$mtrlStdWgtTxt.val(oary.mtrl_std_weight);
		domObj.$mtrlEmpyWgtTxt.val(oary.mtrl_empty_weight);
	}
	
	function initFnc() {
		$("input").attr(VAL.DISABLED_ATTR);
		$("select").attr(VAL.DISABLED_ATTR);
		$("input").val("");
		$("select").empty();
		initMtrlCateSel();
		initMtrlCusd();
//		initMtrlUseMode();
		initMtrlChgQRS();
		initMtrlUnit();
	}
	
	function initMtrlCateSel(){
		comAddValueByDataCateFnc("#mtrlCateSel","MTCT","data_ext","data_desc",true);
	}
	
	function initMtrlCusd(){
		var inObj, outObj;
		var iary = {cus_typ : 'S'};

		inObj = {
				trx_id      : VAL.T_FBPBISCUS,
				action_flg  : 'Q',
					iary	: [iary]
				};
		outObj = comTrxSubSendPostJson(inObj);
		if (outObj.rtn_code == VAL.NORMAL) {
			SelectDom.initWithSpace(domObj.$mtrlMkrSel);
			SelectDom.addSelectArr(domObj.$mtrlMkrSel, outObj.oary, "cus_id", "cus_name", "", true);

		}
	}
	
	function initMtrlUnit(){
		comAddValueByDataCateFnc("#mtrlUnitSel","MTUT","data_ext","data_desc",true);
	}
	
//	function initMtrlUseMode(){
//		comAddValueByDataCateFnc("#mtrlUseModeSel","MTUM","data_ext","data_desc",true);
//	}
	
//	function initMtrlRunMode(){
//		comAddValueByDataCateFnc("#mtrlRunModeSel","TRMD","data_ext","data_desc",true);
//	}
	
//	function initMtrlChgCate(){
//		comAddValueByDataCateFnc("#mtrlChgCateSel","MTCC","data_ext","data_desc",true);
//	}
	
	function initMtrlChgQRS(){
		comAddValueByDataCateFnc("#mtrlQRSCateSel","MQRS","data_ext","data_desc",true);
	}

	function query_func() {

		/** * 将Div实例化为modal窗体 ** */
		function diaLogQueryFnc() {
			var mtrl_prod_id, outObj, iary;

			mtrl_prod_id = $.trim(domObj.dialogs.$queryMtrlDialog_mtrlProdIDTxt.val());
			iary = {};

			if (mtrl_prod_id) {
				iary.mtrl_prod_id = mtrl_prod_id
			}
			inObj_Query = {
				trx_id : VAL.T_FBPBISMTR,
				action_flg : 'Q',
				iary : [iary],
				tbl_cnt : 1
			};
			outObj = comTrxSubSendPostJson(inObj_Query);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#mtrlListGrd");
				domObj.dialogs.$queryMtrlDialog.modal("hide");
				com_get_1st_inf();
			}
		}
		function queryMtrlDialogShowFnc() {
			$("#queryMtrlDialog_mtrlForm input").val("");
		}

		domObj.dialogs.$queryMtrlDialog.modal({
			backdrop : true,
			keyboard : false,
			show : false
		});
		domObj.dialogs.$queryMtrlDialog.unbind('shown.bs.modal');
		domObj.dialogs.$queryMtrlDialog_queryBtn.unbind('click');

		domObj.dialogs.$queryMtrlDialog.bind('shown.bs.modal', queryMtrlDialogShowFnc);
		domObj.dialogs.$queryMtrlDialog.modal("show");
		domObj.dialogs.$queryMtrlDialog_queryBtn.bind('click', diaLogQueryFnc);
		$("#queryMtrlDialog_mtrlProdIDTxt").attr(VAL.ENABLED_ATTR);
	}


	initFnc();
	
	function save_func() {
		var inObj, outObj, iary;
		var action_flg, mtrl_prod_id, mtrl_prod_dsc, mtrl_cate, mtrl_mkr;
		var mtrl_chg_cate, mtrl_use_mode, mtrl_unit, mtrl_use_tool_run_mode, mtrl_qrs_cate;
		var mtrl_std_weight, mtrl_empy_weight, mtrl_safe_stk_qty, evt_usr;
		action_flg = domObj.buttons.$save_btn.data("save_flg");
		if (action_flg == undefined) {
			return false;
		}

		mtrl_prod_id = $.trim(domObj.$mtrlProdIDTxt.val());
		mtrl_prod_dsc = $.trim(domObj.$mtrlProdDscTxt.val());
		mtrl_cate = $.trim(domObj.$mtrlCateSel.find("option:selected").val());
		mtrl_mkr = $.trim(domObj.$mtrlMkrSel.find("option:selected").val());
		mtrl_unit = $.trim(domObj.$mtrlUnitSel.find("option:selected").val());
		mtrl_chg_cate = $.trim(domObj.$mtrlChgCateSel.find("option:selected").val());
		mtrl_use_mode = $.trim(domObj.$mtrlUseModeSel.find("option:selected").val());
		mtrl_use_tool_run_mode = $.trim(domObj.$mtrlRunModeSel.find("option:selected").val());
		mtrl_qrs_cate = $.trim(domObj.$mtrlQRSCateSel.find("option:selected").val());
		mtrl_std_weight = $.trim(domObj.$mtrlStdWgtTxt.val());
		mtrl_empy_weight = $.trim(domObj.$mtrlEmpyWgtTxt.val());
		mtrl_safe_stk_qty = $.trim(domObj.$mtrlSafeWgtTxt.val());
		if (!mtrl_prod_id) {
			showErrorDialog("","物料代码不能为空,请填写");
			return false;
		}
		if (!mtrl_prod_dsc) {
			showErrorDialog("","物料描述不能为空,请填写");
			return false;
		}
		if (!mtrl_cate) {
			showErrorDialog("","物料种类不能为空，请选择");
			return false;
		}
		if (!mtrl_mkr) {
			showErrorDialog("","物料厂商信息不能为空，请选择");
			return false;
		}
		
        if(mtrl_std_weight && !ComRegex.isNumber(mtrl_std_weight)){
            showErrorDialog("","物料标准重量不是有效的数字");
            return false;
        }
        
        if(mtrl_safe_stk_qty && !ComRegex.isNumber(mtrl_safe_stk_qty)){
            showErrorDialog("","安全库存数量不是有效的数字");
            return false;
        }
        
        
		iary = {
			mtrl_prod_id : mtrl_prod_id,
			mtrl_prod_dsc : mtrl_prod_dsc,
			mtrl_cate : mtrl_cate,
			mtrl_mkr : mtrl_mkr,
			mtrl_unit : mtrl_unit,
			mtrl_chg_cate : mtrl_chg_cate,
			mtrl_use_mode : mtrl_use_mode,
			mtrl_use_tool_run_mode : mtrl_use_tool_run_mode,
			mtrl_qrs_cate : mtrl_qrs_cate,
			mtrl_std_weight : mtrl_std_weight,
			mtrl_empy_weight : mtrl_empy_weight,
			mtrl_safe_stk_qty : mtrl_safe_stk_qty
		};
		inObj = {
			trx_id : VAL.T_FBPBISMTR,
			action_flg : 'A',
			iary : [iary],
			evt_usr : VAL.EVT_USR,
			tbl_cnt : 1
		};
		outObj = comTrxSubSendPostJson(inObj);
		if (outObj.rtn_code === VAL.NORMAL) {
			$("input").attr(VAL.DISABLED_ATTR);
			$("select").attr(VAL.DISABLED_ATTR);
			if (action_flg === "A") {
				showSuccessDialog("新增物料成功");
				var newRowID = getGridNewRowID("#mtrlListGrd");
				domObj.grids.$mtrlListGrd.jqGrid("addRowData", newRowID,iary);
				domObj.grids.$mtrlListGrd.setSelection(newRowID, true); 
				mtrlGridSelRowFnc(newRowID);
			} else if (action_flg === "U") {
				var rowid = domObj.grids.$mtrlListGrd.jqGrid('getGridParam','selrow');
				mtrlGridSelRowFnc(rowid);
				showSuccessDialog("物料信息更新成功");
			}
		}
	};

	function add_func() {
		if(domObj.$mtrlProdDscTxt.attr("disabled") != "disabled"){
			showErrorDialog("", "目前处于编辑中，请勿进行其它操作");
			return false;
		}
		$("input").attr(VAL.ENABLED_ATTR);
		$("select").attr(VAL.ENABLED_ATTR);
		$("input").val("");
		$("select").empty();
		initMtrlCateSel();
		initMtrlCusd();
//		initMtrlUseMode();
		initMtrlChgQRS();
		initMtrlUnit();
		domObj.$mtrlProdIDTxt.focus();
		domObj.buttons.$save_btn.data("save_flg", "A");

	}
	function update_func() {
		if(domObj.$mtrlProdDscTxt.attr("disabled") != "disabled"){
			showErrorDialog("", "目前处于编辑中，请勿进行其它操作");
			return false;
		}
        var rowid = domObj.grids.$mtrlListGrd.jqGrid('getGridParam','selrow');
		if(rowid == null){
            showErrorDialog("", "请选择需要修改的物料");
            return false;
		}
		$("input").attr(VAL.ENABLED_ATTR);
		$("select").attr(VAL.ENABLED_ATTR);
		domObj.$mtrlProdIDTxt.attr(VAL.DISABLED_ATTR);
		domObj.$mtrlProdDscTxt.focus();
		domObj.buttons.$save_btn.data("save_flg", "U");
	}
	function delete_func() {
		var inObj, outObj, iary, rowIDs, tblCnt, i,j;
		var rowIDs, rowCnt, i;
		var iaryAry = new Array();
		var mtrl_prod_id = domObj.$mtrlProdIDTxt.val();
		
		if(domObj.$mtrlProdDscTxt.attr("disabled") != "disabled"){
			showErrorDialog("", "目前处于编辑中，请勿进行其它操作");
			return false;
		}
		
		if (!mtrl_prod_id) {
			showErrorDialog("003", "请选择需要删除的原料");
			return false;
		}
		$("#deleteDatacateDialog_deleteDateCateBtn").showCallBackWarnningDialog({
			errMsg : "是否删除原料信息,请确认!!!!",
			callbackFn : function(data) {
				if (data.result == true) {
					iary = {
						mtrl_prod_id : mtrl_prod_id
					};
					inObj = {
						trx_id : VAL.T_FBPBISMTR,
						action_flg : 'D',
						iary : [iary],
						evt_usr : VAL.EVT_USR,
						tbl_cnt : 1
					};
					outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code == VAL.NORMAL) {
						rowIDs = domObj.grids.$mtrlListGrd
								.jqGrid("getDataIDs");
						rowCnt = rowIDs.length;
						for (i = 0; i < rowCnt; i++) {
							rowData = domObj.grids.$mtrlListGrd.jqGrid(
									"getRowData", rowIDs[i]);
							if (mtrl_prod_id == rowData.mtrl_prod_id) {
								domObj.grids.$mtrlListGrd.jqGrid(
										"delRowData", rowIDs[i]);
								break;
							}
						}
						showSuccessDialog("删除原料成功");
						$("input").attr(VAL.DISABLED_ATTR);
						$("select").attr(VAL.DISABLED_ATTR);
						$("input").val("");
					}
				}
			}
		});
	}
	function copy_func(){
		if(domObj.$mtrlProdDscTxt.attr("disabled") != "disabled"){
			showErrorDialog("", "目前处于编辑中，请勿进行其它操作");
			return false;
		}
		$("input").attr(VAL.ENABLED_ATTR);
		$("select").attr(VAL.ENABLED_ATTR);
		domObj.$mtrlProdIDTxt.focus();
		domObj.buttons.$save_btn.data("save_flg", "A");
	}
	function rollback_func(){
		if(domObj.$mtrlProdDscTxt.attr("disabled") == "disabled"){
			showErrorDialog("", "没有编辑，无需撤回");
			return false;
		}
		$("#mtrlConditionForm input").attr(VAL.DISABLED_ATTR);
		$("#mtrlConditionForm select").attr(VAL.DISABLED_ATTR);
		var outObj = comTrxSubSendPostJson(inObj_Query);
		if (outObj.rtn_code === VAL.NORMAL) {
			setGridInfo(outObj.oary, "#mtrlListGrd");
			com_get_1st_inf();
		}
	}
	function clear_func() {
		$("input").val("");
	    SelectDom.setSelect($("select"), "", "");
        SelectDom.setSelect(domObj.$mtrlChgCateSel, "", "");
        SelectDom.setSelect(domObj.$mtrlMkrSel, "", "");
        SelectDom.setSelect(domObj.$mtrlQRSCateSel, "", "");
        SelectDom.setSelect(domObj.$mtrlRunModeSel, "", "");
        SelectDom.setSelect(domObj.$mtrlUnitSel, "", "");
		$("checkbox").removeAttr("checked");
		domObj.grids.$mtrlListGrd.jqGrid("clearGridData");
		$("input").attr(VAL.DISABLED_ATTR);
		$("select").attr(VAL.DISABLED_ATTR);
	}
	
	function com_get_1st_inf(){
		var rowIds = domObj.grids.$mtrlListGrd.jqGrid('getDataIDs');
		if(rowIds.length>0){
			domObj.grids.$mtrlListGrd.setSelection(rowIds[0], true);
		}
	}
	domObj.buttons.$query_btn.click(query_func);
	domObj.buttons.$save_btn.click(save_func);
	domObj.buttons.$delete_btn.click(delete_func);
	domObj.buttons.$update_btn.click(update_func);
	domObj.buttons.$add_btn.click(add_func);
	domObj.buttons.$clear_btn.click(clear_func);
	domObj.buttons.$rollback_btn.click(rollback_func);
	domObj.buttons.$copy_btn.click(copy_func)

    domObjs.W.resize(function() {
        resizeFnc();
    });
    //enter
    /*document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
})